Include "Asset/Maps.bmx"
Include "Asset/Scripts.bmx"
'Asset Types

Const ASSET_VOID : Int = 0
Const ASSET_IMAGE : Int = 1
Const ASSET_SCRIPT : Int = 2
Const ASSET_TILESET : Int = 3
Const ASSET_SPRITE : Int = 4
Const ASSET_MAP : Int = 5
Const ASSET_SOUND : Int = 6
Const ASSET_MUSIC : Int = 7
Const ASSET_OBJECT : Int = 8


Global Asset_List : TList = New TList


Type Asset

	Field FileName : String
	Field FileBank : TBank
	Field AssetParent : Asset
	Field AssetType : Int
	Field AssetPath : String
	Field ShortName : String = "None"
	Field Description : String
	Field Flags : Int

	Method New()

		ListAddLast(Asset_List,Self)

	End Method

	
	Method Save()
	
	
	End Method
	
	Method Load()
	
	End Method
	
	Method FetchAssetPath : String()
	
		If AssetParent = Null Then 
			Return AssetPath
		Else
			Return AssetParent.FetchAssetPath()+AssetPath
			
		EndIf
	End Method
	
	
	Method LoadAsset : Asset(FName : String,NewPath : String= "")
	
		FileName = Replace(FName,"\","/")
		FileBank = LoadBank(FileName)
		
		Select Lower(ExtractExt( FileName ))
		
			Case "jpg","bmp","png"
				AssetType = 1
				Return Self
				
			Case "tset"
				Local tset : TileSet = New TileSet
				tset.FileName = FileName
				tset.FileBank = FileBank
				tset.AssetType = 3
				tset.ShortName = Replace(tset.FileName,ExtractDir( tset.FileName ),"")
				DebugLog "Loading Tileset "+"Projects/"+CurrentProject+NewPath+tset.ShortName
				'tset.AssetPath = "Projects/"+CurrentProject+NewPath+tset.ShortName
				tset.Load()
				Return Asset(tset)
		
			Case "map"
				Local mmap : Map = New Map
				mmap.FileName = FileName
				mmap.FileBank = FileBank
				mmap.AssetType = 4
				mmap.ShortName = Replace(mmap.FileName,ExtractDir( mmap.FileName ),"")
				mmap.AssetPath = NewPath+"/"+mmap.ShortName

				Return Asset(mmap)	
				
			Case "lua"
				Local scr : Script = New Script
				scr.FileName = FileName
				scr.FileBank = FileBank
				scr.AssetType = 2
				scr.ShortName = Replace(scr.FileName,ExtractDir( scr.FileName ),"")
				scr.AssetPath = NewPath+"/"+scr.ShortName

				Return Asset(scr)						
		
			Default
				Local ass : Asset = New Asset
				ass.FileName = FileName
				ass.FileBank = FileBank
				ass.AssetType = 0
				ass.ShortName = Replace(ass.FileName,ExtractDir( ass.FileName ),"")
				ass.AssetPath = NewPath+"/"+ass.ShortName

				Return ass
		End Select
	
	End Method

End Type


Function GenerateTileset : Tileset(imgfile : Object, frms : Int, ww : Int, hh : Int, fflags : Int)
		
		Local TSet : Tileset
		Local bbank : TBank
		
		If imgfile <> Null Then
			TSet = New Tileset
			bbank = LoadBank(imgfile)
		

			TSet.TilesetFromBank(bbank, frms, ww, hh, fflags)
			Return TSet
		Else	
			Return Null
		EndIf

End Function